package com.example.springamqp.basicQueue;

import com.alibaba.fastjson.JSON;
import com.example.Application;

import com.example.config.User;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class BasicQueue implements Serializable {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Test
    public void testSimpleQueue() {
        // 1、准备和交换机 队列名称  和数据
        String queueName = "simple.queue";
        String message = "Hello, SpringAMQP1! ";
//        HashMap<String, String> objectObjectHashMap = new HashMap<>();
//        objectObjectHashMap.put("1","啊啊");
//        objectObjectHashMap.put("2","ee");
        User user = new User(11, "小陈", "男");

        // 2、指定消息具体的参数
        MessageProperties properties = new MessageProperties();
        properties.setHeader("test","测试");
        properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        properties.setContentEncoding("utf-8");

        // 3、设置消息的持久化模式
        properties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);

        // 4、创建消息  消息内容  消息配置
        Message message1 = new Message(JSON.toJSONBytes(user), properties);

        // 5、发送消息 MessagePostProcessor 发送消息之前的消息拦截器  可以对消息参数进行修改
        rabbitTemplate.convertAndSend(queueName, message1, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                return message;
            }
        });
    }

    @Test
    public void testWorkQueue() throws InterruptedException {
        String queueName = "simple.queue";
        String message = "Hello, SpringAMQP! __ ";
        for (int i = 1; i <= 50; i++) {
            // 循环发送50条消息，带上消息编号
            rabbitTemplate.convertAndSend(queueName, message + i);
            // 休眠20ms，模拟在1s内发送完
            Thread.sleep(20);
        }
    }

    @Test
    public void testFanoutExchange() {
        String exchangeName = "fanout.exchange";
        String msg = "fanout来咯";
        rabbitTemplate.convertAndSend(exchangeName, "", msg);
    }

    @Test
    public void testDirectExchange1() {
        String exchangeName = "directExchange";
        String message = "打派派，上号";
        rabbitTemplate.convertAndSend(exchangeName, "all", message);
    }

    @Test
    public void testDirectExchange2() {
        String exchangeName = "directExchange";
        String message = "对a的悄悄话";
        rabbitTemplate.convertAndSend(exchangeName, "a", message);
    }

    @Test
    public void testDirectExchange3() {
        String exchangeName = "directExchange";
        String message = "对b的悄悄话";
        rabbitTemplate.convertAndSend(exchangeName, "b", message);
    }


}
